import request from '@/utils/request'
import type { Room, Result } from '@/types/room'
import type { Card } from '@/types/card'

export interface RoomStatus {
  status: 'WAITING' | 'PLAYING' | 'FINISHED'
  players: Array<{
    id: number
    nickname: string
    score: number
    ready: boolean
    cards?: Card[]
    cardsCount?: number
  }>
  currentRound?: number
  totalRounds?: number
  readyPlayersCount?: number
  totalPlayers?: number
  currentPlayerId?: number
  lastPlayerId?: number
  lastPlayedCards?: Card[]
  myCards?: Card[]
  roundScores?: {[key: number]: number}
}

export interface SubmitHandRequest {
  frontHand: Card[]
  middleHand: Card[]
  backHand: Card[]
}

export const roomApi = {
  createRoom(): Promise<Result<Room>> {
    return request.post('/api/rooms/create')
  },

  getAvailableRooms(): Promise<Result<Room[]>> {
    return request.get('/api/rooms/list')
  },

  getRoom(roomCode: string): Promise<Result<Room>> {
    return request.get(`/api/rooms/${roomCode}`)
  },

  getRoomStatus(roomCode: string): Promise<Result<RoomStatus>> {
    return request.get(`/api/rooms/${roomCode}/status`)
  },

  joinRoom(roomCode: string): Promise<Result<void>> {
    return request.post(`/api/rooms/${roomCode}/join`)
  },

  leaveRoom(roomCode: string): Promise<Result<void>> {
    return request.post(`/api/rooms/${roomCode}/leave`)
  },

  ready(roomCode: string): Promise<Result<void>> {
    return request.post(`/api/rooms/${roomCode}/ready`)
  },

  cancelReady(roomCode: string): Promise<Result<void>> {
    return request.post(`/api/rooms/${roomCode}/cancel-ready`)
  },

  startGame(roomCode: string): Promise<Result<void>> {
    return request.post(`/api/rooms/${roomCode}/start`)
  },

  playCards(roomCode: string, cards: string[]): Promise<Result<void>> {
    return request.post(`/api/rooms/${roomCode}/play`, { cards })
  },

  pass(roomCode: string): Promise<Result<void>> {
    return request.post(`/api/rooms/${roomCode}/pass`)
  },

  deleteRoom(roomCode: string): Promise<Result<void>> {
    return request.delete(`/api/rooms/${roomCode}`)
  },

  submitHand(roomCode: string, data: SubmitHandRequest) {
    return request.post(`/api/rooms/${roomCode}/submit-hand`, data)
  }
} 